package views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;

import com.tetris.project.R;
import com.tetris.project.Shr_Res;

public class WellcomeAnimation extends View implements AnimationListener{

	Bitmap[] piece= new Bitmap[5];
	Paint piecePaint[] = new Paint[5];
	TranslateAnimation wellcome;
	private int i = 0;
	
	public WellcomeAnimation(Context context, AttributeSet attrs) {
		super(context, attrs);

		piecePaint[0] = new Paint();
		piecePaint[1] = new Paint();
		piecePaint[2] = new Paint();
		piecePaint[3] = new Paint();
		piecePaint[4] = new Paint();

		piece[0] = BitmapFactory.decodeResource(getResources(), R.drawable.piece_i);
		piece[1] = BitmapFactory.decodeResource(getResources(), R.drawable.piece_l);
		piece[2] = BitmapFactory.decodeResource(getResources(), R.drawable.piece_cube);
		piece[3] = BitmapFactory.decodeResource(getResources(), R.drawable.piece_s);
		piece[4] = BitmapFactory.decodeResource(getResources(), R.drawable.piece_t);

		piecePaint[0].setAlpha(0);
		piecePaint[1].setAlpha(0);
		piecePaint[2].setAlpha(0);
		piecePaint[3].setAlpha(0);
		piecePaint[4].setAlpha(0);

	}
	
	public WellcomeAnimation(Context context){
		super(context);
	}
	
	private void createAnim(Canvas c){
		
		wellcome = new TranslateAnimation(
			Animation.ABSOLUTE , 0, Animation.ABSOLUTE, 0,
			Animation.ABSOLUTE, 0, Animation.ABSOLUTE, c.getHeight());
		wellcome.setDuration(5000L);
		//wellcome.setRepeatMode(TranslateAnimation.RESTART);
		//wellcome.setRepeatCount(Animation.INFINITE);
    	wellcome.setZAdjustment(TranslateAnimation.ZORDER_BOTTOM);
    	wellcome.setInterpolator(new AccelerateDecelerateInterpolator());
    	wellcome.setStartOffset(0);
    	wellcome.setAnimationListener(this);
    	
    	startAnimation(wellcome);
	}
	
	@Override
	public void onDraw(Canvas c){
		super.onDraw(c);
		
		if (wellcome == null){
			createAnim(c);
		}
		
		c.drawBitmap(piece[0], c.getWidth()/4, 0, piecePaint[0]);
		c.drawBitmap(piece[1], c.getWidth()/4*2, 0, piecePaint[1]);
		c.drawBitmap(piece[2], c.getWidth()/3, 0, piecePaint[2]);
		c.drawBitmap(piece[3], c.getWidth()/10, 0, piecePaint[3]);
		c.drawBitmap(piece[4], c.getWidth()/3*2, 0, piecePaint[4]);
		
	}

	@Override
	public void onAnimationEnd(Animation animation) {
		Log.d(Shr_Res.TAG, "Ended Animation");
		piecePaint[i].setAlpha(0);
		i++;
		if(i >=5){i = 0;}
		startAnimation(wellcome);		
	}

	@Override
	public void onAnimationRepeat(Animation animation) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onAnimationStart(Animation animation) {
		Log.d(Shr_Res.TAG, "Started Animation");
		Log.d(Shr_Res.TAG, "i ="+i);
		piecePaint[i].setAlpha(255);
		
	}
}
